/*
 * @Descripttion:
 * @version:
 * @Author: liyang
 * @Date: 2020-04-01 17:17:45
 * @LastEditors: liyang
 * @LastEditTime: 2020-04-02 14:54:22
 */
var httpReq = require("./libs/http");
const moment = require("moment");
var express = require("express");
var path = require("path");
var fs = require("fs");
var logger = require("morgan");
var cookieParser = require("cookie-parser");
var bodyParser = require("body-parser");
var app = express();

var config = require("./config.default.js");

const accessKey = "dingoarhla5gdknnn6fll2"; //appid

const crypto = require("./libs/crypto");

app.use(logger("dev"));
app.use(bodyParser.json());
app.use(
  bodyParser.urlencoded({
    extended: false
  })
);
app.use(cookieParser());

HttpUtils = new httpReq(config.oapiHost);

// 获取用户信息
app.use("/login", function(req, res) {
  // 获取access_token
  HttpUtils.get(
    "/gettoken",
    {
      appkey: config.appkey,
      appsecret: config.appsecret
    },
    function(err, body) {
      if (!err) {
        var code = req.body.authCode;
        var accessToken = body.access_token;
        //获取用户id
        HttpUtils.get(
          "/user/getuserinfo",
          {
            access_token: accessToken,
            code: code
          },
          function(err2, body2) {
            if (!err2) {
              //获取用户详细信息
              HttpUtils.get(
                "/user/get",
                {
                  access_token: accessToken,
                  userid: body2.userid
                },
                function(err3, body3) {
                  console.log("body2--->", body2);
                  console.log("body3--->", body3);
                  if (!err3) {
                    res.send({
                      result: {
                        userId: body2.userid,
                        userName: body3.name
                      }
                    });
                  } else {
                    console.err("获取用户信息失败");
                  }
                }
              );
            } else {
              console.err("获取用户id失败");
            }
          }
        );
      } else {
        console.err("获取access_token失败");
      }
    }
  );
});

app.use("/callback", function(req, res) {
  console.log("code--->  ", req.query);
  const code = req.query.code;
  //   const timestamp = moment(new Date()).valueOf() + "";
  //   const signature = crypto.sha256(timestamp, accessKey);

  const sign = crypto.signature(accessKey);
  const signature = sign.sign;
  const timestamp = sign.timestamp;
  
  console.log(
    `curl 'https://oapi.dingtalk.com/sns/getuserinfo_bycode?signature=${signature}&timestamp=${timestamp}&accessKey=${accessKey}' -H "Content-Type:application/json" -X POST --data '{"tmp_auth_code":"${code}"}'`
  );

  HttpUtils.post(
    `https://oapi.dingtalk.com/sns/getuserinfo_bycode?signature=${signature}&timestamp=${timestamp}&accessKey=${accessKey}`,
    {},
    {
      tmp_auth_code: code
    },
    function(err, body) {
      //   console.log("body--->", body);
      if (!err) {
        res.send({
          result: body
        });
      } else {
        res.send({
          result: err
        });
      }
    }
  );
});

app.use(function(req, res, next) {
  res.send("welcome");
});

module.exports = app;
